﻿using KeDa2025_.Utils;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Security.Principal;
using System.Web.UI;
namespace KeDa2025_
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IninCity();
            if (!IsPostBack)
            {
                int id;
                if (int.TryParse(Request["id"], out id))
                {
                    DbHelper db = new DbHelper();
                    // 构建 SQL 查询，避免使用参数化 SQL 的兼容性问题
                    string sql = $"SELECT * FROM user WHERE id = {id}";
                    DataSet ds = DbHelper.Query(sql);

                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow row = ds.Tables[0].Rows[0];
                        txtAccount.Text = row["account"].ToString();
                        txtBirday.Text = row["birthday"].ToString();
                        txtPwd.Text = row["pwd"].ToString();
                        ddlCity.SelectedValue = row["city_id"].ToString();
                        ratnGender.SelectedValue = row["gender"].ToString();
                        Image1.ImageUrl = row["img_url"].ToString();
                        txtPhone.Text = row["phone"].ToString();
                        txtEmail.Text = row["email"].ToString();
                        txtQq.Text = row["qq"].ToString();
                    }
                }
            }
        }
        public void IninCity() {

            string sql = string.Format("select * from province");
            MySqlConnection connection = new MySqlConnection("server=localhost;user id=zmy2;password=zmy20020525;database=forum;");
            MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, connection);
            DataSet ds = new DataSet();
            // 填充数据集
            sqlDataAdapter.Fill(ds);
            //DataSet ds = DbHelper.Query("select * from province");
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "name";
            ddlCity.DataValueField = "id";
            ddlCity.DataBind();
     
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            // 只能上传为图片
            string exList = ".jpg.png.gif";
            // 1. 捕获文件名称
            string imgname = fileUpImg.FileName; // a.img

            int dotIndex = imgname.LastIndexOf(".");
            if (dotIndex < 0)
            {
                Response.Write("<script>alert('文件格式不正确，支持的格式为 .jpg, .png, .gif');</script>");
                return;
            }

            string exName = imgname.Substring(dotIndex);

            if (!exList.Contains(exName))
            {
                Response.Write("<script>alert('仅支持上传图片格式');</script>");
                return;
            }

            if (fileUpImg.FileContent.Length > 1 * 1024 * 1024)
            {
                Response.Write("<script>alert('图片大小超过1MB');</script>");
                return;
            }

            // 2. 确定文件保存的物理地址
            string path = Server.MapPath("images") + "\\" + imgname;

            // 3. 进行文件保存
            fileUpImg.SaveAs(path);
            Image1.ImageUrl = "/images/" + imgname; // "/img/a.jpg"
        }
    

        protected void btLogin_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request["id"]);
            string account = txtAccount.Text.Trim();
            string birthday=txtBirday.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            int city = Convert.ToInt32(ddlCity.SelectedValue);
            int gender = Convert.ToInt32(ratnGender.SelectedValue);
            string image = Image1.ImageUrl;
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq= txtQq.Text.Trim();

            //增加数据库语句           
          string sql = string.Format("INSERT INTO users (account,birthday,pwd,city_id,gender,img_url,phone,email,qq) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}')", account, birthday, pwd,city,gender,image,phone,email,qq);
            // 建立数据库连接
            string connectionString = "server=localhost;user id=zmy2;password=zmy20020525;database=forum;";
            MySqlConnection connection = new MySqlConnection(connectionString);

            // 创建MySQLCommand对象
            MySqlCommand cmd = new MySqlCommand(sql, connection);

            // 执行SQL语句并获取结果
            connection.Open();
            int flag = cmd.ExecuteNonQuery();
            connection.Close();
            if (flag > 0)
                Response.Write("<script>alert('注册成功');window.location.href='index.aspx'</script>");





        }
    }
}